﻿using System;
using System.Reflection;
using nRoute.Components.Composition;

namespace Orktane.ScriptActionModel
{
    public class ScriptActionMeta
    {
        readonly Type _implementationType;
        readonly string _prefix;
        readonly string _title;
        readonly string _shortDescription;
        readonly string _additionalDescription;
        readonly Uri _moreInformationUri;
        readonly InstanceLifetime _lifetime;
        readonly bool _unListed;

        public ScriptActionMeta(Type implementationType, string prefix, InstanceLifetime lifetime, 
            string title, string shortDescription, string additionalDescription, Uri moreInformationUrl, bool unListed)
		{

            if (implementationType == null) throw new ArgumentNullException("implementationType");
			if (string.IsNullOrEmpty(prefix)) throw new ArgumentNullException("prefix");
            if (string.IsNullOrEmpty(title)) throw new ArgumentNullException("title");

            // we save
			_implementationType = implementationType;
            _prefix = prefix.ToUpper();
            _lifetime = lifetime;
            _title = title;
            _additionalDescription = additionalDescription;
            _shortDescription = shortDescription;
			_moreInformationUri = moreInformationUrl;
            _unListed = unListed;
		}

#region Properties

        public Type ImplementationType
		{
			get { return _implementationType; }	
		}
		
        public string Prefix 
		{
			get { return _prefix; }	
		}

        public InstanceLifetime Lifetime
        {
            get { return _lifetime; }
        }

        public string Title
		{
			get { return _title; }
        }

        public string ShortDescription
        {
            get { return _shortDescription; }
        }

        public string AdditionalDescription
		{
			get { return _additionalDescription; }	
		}

        public Uri MoreInformationUrl
		{
			get { return _moreInformationUri; }	
		}

        public bool UnListed
        {
            get { return _unListed; }
        }

        public string Culture
		{
			get { return  GetAssemblyAttribute<AssemblyCultureAttribute>().Culture; }	
		}

        public string Company
		{
			get { return  GetAssemblyAttribute<AssemblyCompanyAttribute>().Company; }	
		}

        public string Copyright
		{
			get { return  GetAssemblyAttribute<AssemblyCopyrightAttribute>().Copyright; }	
		}

        public string Trademark
		{
			get { return  GetAssemblyAttribute<AssemblyTrademarkAttribute>().Trademark; }
		}

        public string Version
		{
			get { return  GetAssemblyAttribute<AssemblyVersionAttribute>().Version; }	
		}

        public string FileVersion
		{
			get { return  GetAssemblyAttribute<AssemblyFileVersionAttribute>().Version; }	
		}

#endregion
		
#region Helper
		
		T GetAssemblyAttribute<T>()
			where T : Attribute
		{
			var _attributes = _implementationType.Assembly.GetCustomAttributes(typeof(T), false);
			return _attributes != null && _attributes.Length > 0 ? (T)_attributes[0] : null;
		}
		
#endregion
		
	}
}